import 'package:flutter/cupertino.dart';
import 'package:flutter_base_app/page/view/main_page.dart';
import 'package:flutter_base_framework/base_framework_export.dart';
import 'package:get/get.dart';

import '../page/view/other/h5_page.dart';
import '../page/view/test_page.dart';
import '../page/view/welcome_page.dart';
import '../widgets/dialog/base/base_dialog.dart';

/// 路由key
class RouterKey {
  RouterKey._();

  static const String welcome = '/';
  static const String h5 = '/h5';
  static const String imageCrop = '/image/crop';
  static const String main = '/main';
  static const String home = '/home';
  static const String login = '/login';
  static const String register = '/register';
  static const String forgotPassword = '/forgotPassword';
  static const String updatePassword = '/updatePassword';
  static const String cmdTest = '/cmd/test';
  static const String test = '/test';
  static const String log = '/log';
  static const String unknown = '/404';
  static const String userInfo = '/user/info';
}

class RouterParams {
  static const String bool = 'bool';
}

/// 路由跳转

/// 路由跳转
class RouterJump {
  RouterJump._();

  static List<Route<dynamic>> get history => routeRecord.history;

  ///获取当前页面参数
  static get arguments => Get.arguments ?? {};

  ///获取当前页面参数
  static Map<String, String?> get parameters => Get.parameters;

  static Route? get lastRoute => history.lastOrNull;

  static bool? isCurrentOf(BuildContext context) {
    try {
      return ModalRoute.isCurrentOf(context) == true;
    } catch (e) {
      return null;
    }
  }

  ///通过name获取route
  static List<Route> getRoutesByName(String name) {
    return history.where((element) => element.settings.name == name).toList();
  }

  ///通过name获取route，从栈顶开始查找
  static Route? getRouteByName(String name) {
    var index = history.lastIndexWhere((element) => element.settings.name == name);
    if (index != -1) {
      return history[index];
    }
    return null;
  }

  //返回
  static void back({dynamic result}) {
    Get.back(result: result, closeOverlays: true);
  }

  static void finish(String name) {
    //关闭指定页面
    if (Get.currentRoute == name) {
      Get.back(closeOverlays: true);
      return;
    }
    //如果是中间页面，就使用历史记录查询Route，然后删除
    var route = getRouteByName(name);
    if (route != null) {
      Get.removeRoute(route);
    }
    // var routes = getRoutesByName(name);
    // for (var o in routes) {
    //   Get.removeRoute(o);
    // }
  }

  /// 根据接口跳转h5
  /// @param number 多个h5选择的key 如
  /// @param type 1=隐私协议(number 1:使用条款，2：隐私政策) 2=Help & Guide,3:支付

  static void startHttpH5(String type, {int number = 1, Map<String, dynamic>? otherParams}) async {
    // final result = await ApiMain.dicH5(type, HttpUiHandler.get());
    // if (result.isSucceed) {
    //   String url = '';
    //   if (type == "1") {
    //     url = result.getXAsT(['data', number == 1 ? 'terms_of_service' : 'privacy_notice']);
    //   } else {
    //     url = result.getXAsT(['data', 'value']);
    //   }
    //   if (url.isNotEmpty) {
    //     final params = otherParams ?? {};
    //     params['dayNight'] = true;
    //     startH5(url, otherParams: params);
    //   }
    // }
  }

  //启动H5
  static void startH5(String url, {String? title, Map<String, dynamic>? otherParams}) {
    if (url.isEmpty) return;
    Get.toNamed(RouterKey.h5, arguments: {'url': url, 'title': title, 'otherParams': otherParams});
  }

  //启动图片裁剪
  static Future<dynamic>? imageCrop(String filePath) {
    return Get.toNamed(RouterKey.imageCrop, arguments: {'filePath': filePath});
  }

  static void login() {
    // Navigator.push(
    //   AppRoutes.currentContext,
    //   MaterialPageRoute(builder: (context) => LoginPage()),
    // );
    //使用Push 前面页面不会销毁
    //使用Go  前面页面会销毁
    // AppRoutes.currentContext?.pushNamed(AppRouteKey.login);
    if (Get.currentRoute != RouterKey.login) {
      //关闭全部对话框
      dismissAllDialog();
      Get.offAllNamed(RouterKey.login);
    }
  }


  //注册
  static void register(String userName) {
    Get.toNamed(RouterKey.register, arguments: {'userName': userName});
  }

  //忘记密码
  static void forgotPassword(String userName) {
    Get.toNamed(RouterKey.forgotPassword, arguments: {'userName': userName});
  }

  //修改密码
  static void updatePassword(String userName) {
    Get.toNamed(RouterKey.updatePassword, arguments: {'userName': userName});
  }

  //是否当前页面
  static bool isCurrentRoute(String routeName) {
    return Get.currentRoute == routeName;
  }

  //主页
  static void main() {
    if (Get.currentRoute != RouterKey.main) {
      //关闭全部对话框
      dismissAllDialog();
      Get.offAllNamed(RouterKey.main);
    }
  }

  //主页是否在路由中 存在
  static bool mainExistRoute() {
    return getRouteByName(RouterKey.main) != null;
  }

  //测试页面
  static void test() {
    Get.toNamed(RouterKey.test);
  }




  //用户信息页面
  static void userInfo() {
    Get.toNamed(RouterKey.userInfo);
  }



}

class AppRoutes {
  //全局导航key，不用BuildContext
  static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  static BuildContext? get currentContext => navigatorKey.currentContext;
  static final routes = [
    GetPage(
      name: RouterKey.welcome,
      page: () => const WelcomePage(),
      transition: Transition.noTransition,
    ),
    // GetPage(
    //   name: RouterKey.login,
    //   page: () => const LoginPage(),
    //   transition: Transition.downToUp,
    // ),
    // GetPage(
    //   name: RouterKey.register,
    //   page: () => const RegisterPage(),
    // ),
    // GetPage(
    //   name: RouterKey.forgotPassword,
    //   page: () => const ForgotPasswordPage(),
    // ),
    // GetPage(
    //   name: RouterKey.updatePassword,
    //   page: () => const UpdatePasswordPage(),
    // ),
    GetPage(
      name: RouterKey.h5,
      page: () => const H5Page(),
    ),
    GetPage(
      name: RouterKey.main,
      page: () => const MainPage(),
      transition: Transition.noTransition,
    ),

    GetPage(
      name: RouterKey.test,
      page: () => const TestPage(),
    ),
  ];

  static void init() {}
}
